package handler

import (
	// "demo/internal/logic"
	"demo/internal/logic"
	"demo/internal/svc"
	"demo/internal/types"
	"demo/model"
	"net/http"

	"github.com/zeromicro/go-zero/rest/httpx"
)



func EnrollHandler(svcCtx *svc.ServiceContext)http.HandlerFunc{
	return func(w http.ResponseWriter,r *http.Request){
		var req types.EnrollReq

		if err := httpx.Parse(r,&req);err != nil{
			httpx.ErrorCtx(r.Context(),w,err)
			return
		}
		remoteAddr := r.RemoteAddr

		user := &model.User{
			LastLoginIp: remoteAddr,
			Username: req.Username,
			Password: req.Password,
			Mobile: req.Mobile,
		}

		l := logic.NewEnrollLogic(r.Context(),svcCtx)
		
		resp,err := l.Enroll(user)

		if err != nil{
			httpx.ErrorCtx(r.Context(),w,err)
		}else{
			httpx.OkJsonCtx(r.Context(),w,resp)
		}
	}
}